%%%-------------------------------------------------------------------
%%% @author liuwentao
%%% @doc
%%%     角色登录模块
%%% @end
%%% Created : 15. 7月 2021 15:14
%%%-------------------------------------------------------------------
-module(role_login).
-author("liuwentao").

-export([
    do/1
    , delay_do/1
]).


-include("common.hrl").
-include("role.hrl").

%% 列表中的函数及返回格式要求如下:
%% fun(Role :: #role{}) -> ok | {ok, NewRole :: #role{}}.
-define(fun_list, [

]).

%% 延迟执行
-define(fun_list_delay, [

]).

%% ----------------------------------------------------
%% 外部接口
%% ----------------------------------------------------
%% 执行登录接口
do(Role) -> do_do(?fun_list, Role).

%% @doc 延迟执行
delay_do(Role = #role{}) ->
    do_do(?fun_list_delay, Role).

%% ----------------------------------------------------
%% 私有函数
%% ----------------------------------------------------

%% 执行调用
do_do([], Role) -> {ok, Role};
do_do([{M, F} | T], Role = #role{name = Name}) ->
    case catch erlang:apply(M, F, [Role]) of
        ok ->
            do_do(T, Role);
        {ok, NewRole} when is_record(NewRole, role) ->
            do_do(T, NewRole);
        {false, Reason} ->
            ?ERR("角色[~ts]account [~ts]执行登录调用[~w:~w]时发生异常: ~w", [Name, Role#role.account, M, F, Reason]),
            {error, Reason};
        Err ->
            ?ERR("角色[~ts]account [~ts]执行登录调用[~w:~w]时发生异常: ~w", [Name, Role#role.account, M, F, Err]),
            {error, ?T("登录出错")}
    end.
